package com.lyh.hkcloud.module.m1300028;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.activity.HomeActivity;
import com.lyh.hkcloud.base.BaseActivity;
import com.lyh.hkcloud.constants.AppConstants;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.constants.HttpUrlComstants;
import com.lyh.hkcloud.module.m1300027.service.Service;
import com.lyh.hkcloud.module.m1300028.adapter.ListViewAdapter;
import com.lyh.hkcloud.util.base.*;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description: 盘点差异
 * @company—date: 利元数据 2022-02-18 17:07
 **/
public class IndexActivity extends BaseActivity implements View.OnClickListener{

    private ListView lvAssets;
    private ListViewAdapter listViewAdapter;
    private ImageView goBack;
    private Button bt_save;
    List<HashMap<String,Object>> dataList = new ArrayList<>();

    JSONArray result =new JSONArray();
    private Service service;

    private String data;

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_1300028;
    }

    @Override
    protected void initView() {
        lvAssets=findViewById(R.id.lv_assets);
        goBack = findViewById(R.id.go_back);
        goBack.setOnClickListener(this);
        bt_save=findViewById(R.id.bt_save);
        getSupportActionBar().hide();

        data = getIntent().getStringExtra(BusinessConstants.DATA);
    }
    @Override
    protected void onBeginInit() {
        service = new Service(IndexActivity.this);
    }

    @Override
    protected int getSubmitResId() {
        return R.id.bt_save;
    }

    @Override
    protected void onSubmit() {
        bt_save.setEnabled(false);
        JSONObject inMap=new JSONObject();
        try {
            //盘点单号
            inMap.put("dataid", new JSONObject(data).getString(BusinessConstants.INVENTORY_ID));
            inMap.put("userid", SaveData.getString(this,"userid"));
            inMap.put("username", SaveData.getString(this,"userName"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        service.post(inMap, HttpUrlComstants.FA_MA_INV_DIF_CLOSESAVE, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Intent intent =new Intent(IndexActivity.this, com.lyh.hkcloud.module.m1300026.IndexActivity.class);
                //intent.putExtra(BusinessConstants.DATA,data);
                IndexActivity.this.startActivity(intent);
                //onClick(goBack);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_back:
                Intent intent =new Intent(IndexActivity.this, com.lyh.hkcloud.module.m1300027.IndexActivity.class);
                intent.putExtra(BusinessConstants.DATA,data);
                IndexActivity.this.startActivity(intent);
                break;
        }
    }


    @Override
    protected void initEvent() {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //
        inventoryVariance();
        //接受页面传过来的盘点差异数据
//        Intent intent=getIntent();
//        String dataString=intent.getStringExtra("dataArray");
//        //盘点编号
//        String inventoryID=intent.getStringExtra(BusinessConstants.INVENTORY_ID);
//        try {
//            if(!StringUtils.isEmpty(dataString)){
//                JSONObject jsonObject =new JSONObject(dataString);
//                //盘点差异列表
//                result = JsonUtil.getJsonObjectArray(jsonObject, "result");
//            }
//            else{
//                this.inventoryVariance();
//            }
//            List<HashMap<String,Object>> dataList = new ArrayList<>();
//            for(int i=0;i<result.length();i++){
//                HashMap<String,Object>data=new HashMap<>();
//                data.put(BusinessConstants.FA_TYPE,result.getJSONObject(i).get(BusinessConstants.FA_TYPE));
//                data.put(BusinessConstants.LINE_STATE,result.getJSONObject(i).get(BusinessConstants.LINE_STATE));
//                data.put(BusinessConstants.MAN_UMBER,result.getJSONObject(i).get(BusinessConstants.MAN_UMBER));
//                dataList.add(data);
//            }
//            listViewAdapter = new ListViewAdapter(this, dataList,inventoryID,new JSONObject(data).getString(BusinessConstants.INVENTORY_STATUS));
//            lvAssets.setAdapter(listViewAdapter);
//            //回传参数给上一页
//            onClick(goBack);
////            Intent previousPageData=new Intent();
////            previousPageData.putExtra(BusinessConstants.DATA,data);
////            // 返回数据，第一个参数是响应码，，参数2是把带有参数的Intent传递进去
////            this.setResult(BusinessConstants.requestCode,previousPageData);
//            //this.finish();
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * @Description 查询盘点差异
     * @param
     * @return
     * @Date 2022/2/20 15:48
     * @author 黄斌成
     **/
    private void inventoryVariance(){
        JSONObject inMap=new JSONObject();
        try {
            //盘点单id
            inMap.put("dataid", new JSONObject(data).getString(BusinessConstants.INVENTORY_ID));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        service.post(inMap, HttpUrlComstants.QUERY_FA_MA_INV_LINE_DIF, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                try {

                    result = JsonUtil.getJsonObjectArray(response, "result", IndexActivity.this);
                    //List<HashMap<String,Object>> dataList = new ArrayList<>();
                    for(int i=0;i<result.length();i++){
                        HashMap<String,Object>data=new HashMap<>();
                        data.put(BusinessConstants.FA_ID,result.getJSONObject(i).get("sn"));
                        data.put(BusinessConstants.FA_TYPE,result.getJSONObject(i).get(BusinessConstants.FA_TYPE));
                        data.put(BusinessConstants.LINE_STATE,result.getJSONObject(i).get(BusinessConstants.LINE_STATE));
                        data.put(BusinessConstants.MAN_UMBER,result.getJSONObject(i).get(BusinessConstants.MAN_UMBER));
                        data.put(BusinessConstants.LINESTATE, result.getJSONObject(i).get(BusinessConstants.LINESTATE));
                        data.put(BusinessConstants.SYS_QTY, result.getJSONObject(i).get(BusinessConstants.SYS_QTY));
                        data.put(BusinessConstants.INV_QTY, result.getJSONObject(i).get(BusinessConstants.INV_QTY));
                        dataList.add(data);
                    }
                    //盘点编号
                    listViewAdapter = new ListViewAdapter(IndexActivity.this, dataList,new JSONObject(data).getString(BusinessConstants.INVENTORY_ID),new JSONObject(data).getString(BusinessConstants.INVENTORY_STATUS));
                    lvAssets.setAdapter(listViewAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }
}
